from bs4 import BeautifulSoup

soup = BeautifulSoup(open('bs.html','r',encoding='utf8'),'lxml')
#获取标签
print(soup.a)
#获取标签的内容
print(soup.a.attrs)
#获取符合条件的数据
print(soup.find('a'))
#根据title的值，获取对应的标签对象
print(soup.find('a',title='a2'))
#根据class的值找到对应的标签对象
print(soup.find('a',class_='c1'))
print("--------------")
# find_all 返回所有的内容列表数据
print(soup.find_all('a'))
# find_all 查找多个数据
print(soup.find_all(['a','span']))
# limit 查找前几个
print(soup.find_all('li',limit=2))
print("select 使用")
print(soup.select('a'))
print(soup.select('.c1'))
print(soup.select('#l1'))
#查找带有id的属性标签
print('------------------------')
print(soup.select('li[id]'))
#层级选择器
print("---------------")
print(soup.select('div li'))
print(soup.select('div > ul > li'))
print(soup.select('a,li'))
#获取节点内容
obj = soup.select('#d1')[0]
print(obj.string)
print(obj.get_text())
print("------------")
#获取节点的属性值
obj = soup.select('#p1')[0]
print(obj.name)
print(obj.attrs)
print(obj.attrs.get('class'))
print(obj.get('class'))